import { _decorator, Component, Label, Node } from 'cc';
import { EnergyModel } from '../Dialog/EnergyModel';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { GameParams } from '../../Basic/Config/DataConfig';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
const { ccclass, property } = _decorator;

@ccclass('HeadBar')
export class HeadBar extends Component {
  @property(Node)
  coinNode: Node;
  @property(Node)
  energyNode: Node;

  protected onLoad(): void {
    EventManager.Ins().register(EventType.REFRESH_COIN, this.refreshCoin, this);
    EventManager.Ins().register(EventType.REFRESH_ENERGY, this.refreshEnergy, this);
  }

  protected onEnable(): void {
    this.refreshCoin();
    this.refreshEnergy();
  }

  refreshCoin() {
    const coin = DataMgr.gameParams.get(GameParams.coin);
    const coin_count = this.coinNode.getChildByName('count').getComponent(Label);
    coin_count.string = `${coin}`;
  }

  refreshEnergy() {
    const energy = DataMgr.gameParams.get(GameParams.energy);
    const energy_count = this.energyNode.getChildByName('count').getComponent(Label);
    const energy_time = this.energyNode.getChildByName('time').getComponent(Label);

    if (DataMgr.gameParams.get(GameParams.energy_award) >= 3) {
      energy_count.string = `∞`;
      energy_time.string = '';
    } else {
      energy_count.string = `${energy} / 5`;
      if (energy >= 5) {
        energy_time.string = ``;
      } else {
        // 开启倒计时
      }
    }
  }

  onClickEneryAdd() {
    EnergyModel.Ins().Show();
  }
}
